package com.stylefeng.guns.modular.charon.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.common.persistence.dao.AccountIpMapMapper;
import com.stylefeng.guns.common.persistence.dao.PcServerInfoMapper;
import com.stylefeng.guns.common.persistence.model.PcServerInfo;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.charon.service.IPcServerInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.common.persistence.model.AccountIpMap;
import com.stylefeng.guns.modular.charon.service.IAccountIpMapService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户账号映射维护控制器
 *
 * @author fengshuonan
 * @Date 2018-02-02 10:59:47
 */
@Controller
@RequestMapping("/accountIpMap")
public class AccountIpMapController extends BaseController {

    private String PREFIX = "/charon/accountIpMap/";

    @Autowired
    private IAccountIpMapService accountIpMapService;
    @Resource
    IPcServerInfoService pcServerInfoService;
    @Resource
    AccountIpMapMapper accountIpMapMapper;

    /**
     * 跳转到用户账号映射维护首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "accountIpMap.html";
    }

    /**
     * 跳转到添加用户账号映射维护
     */
    @RequestMapping("/accountIpMap_add")
    public String accountIpMapAdd() {
        return PREFIX + "accountIpMap_add.html";
    }

    /**
     * 跳转到修改用户账号映射维护
     */
    @RequestMapping("/accountIpMap_update/{accountIpMapId}")
    public String accountIpMapUpdate(@PathVariable Integer accountIpMapId, Model model) {
        AccountIpMap accountIpMap = accountIpMapService.selectById(accountIpMapId);
        model.addAttribute("item",accountIpMap);
        LogObjectHolder.me().set(accountIpMap);
        return PREFIX + "accountIpMap_edit.html";
    }

    /**
     * 获取用户账号映射维护列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String account, String pcServerName) {
        return accountIpMapMapper.list(account,pcServerName);
    }

    /**
     * 新增用户账号映射维护
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(AccountIpMap accountIpMap) {
        accountIpMapService.insert(accountIpMap);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除用户账号映射维护
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer accountIpMapId) {
        accountIpMapService.deleteById(accountIpMapId);
        return SUCCESS_TIP;
    }

    /**
     * 修改用户账号映射维护
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(AccountIpMap accountIpMap) {
        accountIpMapService.updateById(accountIpMap);
        return super.SUCCESS_TIP;
    }

    /**
     * 用户账号映射维护详情
     */
    @RequestMapping(value = "/detail/{accountIpMapId}")
    @ResponseBody
    public Object detail(@PathVariable("accountIpMapId") Integer accountIpMapId) {
        return accountIpMapService.selectById(accountIpMapId);
    }

//    -------------------
    @RequestMapping(value = "/synchroAccount", method = RequestMethod.POST)
    @ResponseBody
    public Object synchroAccount(@RequestBody JSONObject synchroJson) {
//        {ip:'xxx', user:[], synchroType}
        JSONObject obj = synchroJson;
//        JSONObject obj = JSONObject.parseObject(synchroJson);
        String ip = obj.getString("ip");

        PcServerInfo pcServerInfo = new PcServerInfo();
        pcServerInfo = pcServerInfo.selectOne("ip={0} and status='1'", ip);

        if(ToolUtil.isEmpty(pcServerInfo)){
            return new ErrorTip(400,"云端没有当前IP对应的服务器，请先注册服务器!");
        }

        String id = pcServerInfo.getId();
        String synchroType = obj.getString("synchroType");

        JSONArray arr = obj.getJSONArray("user");

        AccountIpMap user = null;
        for(int i=0; i<arr.size(); i++){
            JSONObject json = arr.getJSONObject(i);
            user = new AccountIpMap();
            String phone = json.getString("phone");
            user.setId(phone);
            user.setAccount(json.getString("account"));
            user.setPhone(phone);
            user.setPcId(id);
            user.setSynchroType(synchroType);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.insertOrUpdate();
        }

        return super.SUCCESS_TIP;
    }


}
